package com.usian.behavior.config.kafkaListener;

import com.alibaba.fastjson.JSONObject;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.common.constant.behavior.FollowBehaviorConstants;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;


import java.util.Optional;

/**
 * ClassName: WemediaNewsAutoListener
 * Author:   WGG
 * Date:    2022-11-2022/11/24-21:44
 * Version: 1.0
 * Description:
 */
@Component
public class BehaviorAutoListener {
    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;
    //kakfa监听的主题
    @KafkaListener(topics = FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void recevieMessage(ConsumerRecord<?,?> record) throws Exception {
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            Object value = record.value();
            FollowBehaviorDto followBehaviorDto = JSONObject.parseObject((String) value, FollowBehaviorDto.class);
            //调用自动设置的方法
            apFollowBehaviorService.forwardBehavior(followBehaviorDto);
        }

    }
}